package com.example.mapper;

import com.example.entity.SysRole;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface RoleMapper {

    @Select({"select id, role_name roleName, enabled, create_by createBy, create_time createTime",
            "from sys_role",
            "where id=#{id}"})
    SysRole selectById(Integer id);

    // 也可以写成下面这种形式：
    @Select({"select id, role_name roleName, enabled, create_by createBy, create_time createTime from sys_role where id=#{id}"})
    SysRole selectById2(Integer id);

    // 也可以写成下面这种形式：
    @Select("select id, role_name roleName, enabled, create_by createBy, create_time createTime from sys_role where id=#{id}")
    SysRole selectById3(Integer id);

    @Results(id = "roleResultMap",
            value = {
            @Result(column = "id", property = "id", id = true),
            @Result(column = "role_name", property = "roleName"),
            @Result(column = "enabled", property = "enabled"),
            @Result(column = "create_by", property = "createBy"),
            @Result(column = "create_time", property = "createTime")
    })
    @Select("select id, role_name, enabled, create_by, create_time from sys_role where id=#{id}")
    SysRole selectById_2(Integer id);

    @ResultMap("roleResultMap")
    @Select("select * from sys_role")
    List<SysRole> selectAll();


    @Insert({" insert into sys_role (id, role_name, enabled, create_by, create_time )",
            " values(#{id}, #{roleName}, #{enabled}, #{createBy} ,",
            "#{createTime , jdbcType=TIMESTAMP})"
    })
    int insert(SysRole sysRole);


    @Insert({" insert into sys_role (role_name, enabled, create_by, create_time )",
            " values(#{roleName}, #{enabled}, #{createBy} ,",
            "#{createTime , jdbcType=TIMESTAMP})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert_2(SysRole sysRole);

    @Insert({" insert into sys_role (role_name, enabled, create_by, create_time )",
            " values(#{roleName}, #{enabled}, #{createBy} ,",
            "#{createTime , jdbcType=TIMESTAMP})"
    })
    @SelectKey(
            statement = "select last_insert_id()",
            keyProperty = "id",
            before = false,
            resultType = Long.class
    )
    int insert_3(SysRole sysRole);

    @Update({
            "update sys_role ",
            "set role_name=#{roleName}",
            "    enabled=#{enabled}",
            "    create_by=#{createBy}",
            "    create_time=#{createTime, jdbcType=TIMESTAMP}",
            "where id = #{id}"
    })
    int updateById(SysRole role);


    @Delete("delete from sys_role where id = #{id}")
    int deleteById(Long id);
}
